package com.example.aidldemoservice;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import androidx.core.app.NotificationCompat;

import com.example.aidlsdk.ICalculator;
import com.example.aidlsdk.bean.Sample;

public class CalculatorService extends Service {
    private static final String TAG = "CalculatorService";

    private static final String CHANNEL_ID_STRING = "com.example.aidldemoservice";
    private static final int CHANNEL_ID= 0x11;

    private static final int NOTIFICATION_ID = 1;
    private static final String CHANNEL_ID1 = "ForegroundServiceChannel";

    public CalculatorService() {
    }

    ICalculator.Stub mICalculator = new ICalculator.Stub() {
        @Override
        public int add(int a, int b) throws RemoteException {
            Log.d(TAG, "add: ");
            return a+b;
        }

        @Override
        public int subtract(int a, int b) throws RemoteException {
            Log.d(TAG, "subtract: ");
            return 0;
        }

        @Override
        public int multiply(int a, int b) throws RemoteException {
            Log.d(TAG, "multiply: ");
            return 0;
        }

        @Override
        public int divide(int a, int b) throws RemoteException {
            Log.d(TAG, "divide: ");
            return 0;
        }

        @Override
        public void optionParcel(Sample sampel) throws RemoteException {
            Log.d(TAG, "optionParcel: Sample is "+sampel.toString());
        }

    };

    @Override
    public void onCreate() {
        super.onCreate();
        createNotificationChannel();

        Log.d(TAG, "onCreate: ");
    }


    @SuppressLint("ForegroundServiceType")
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // 启动前台服务必须在前5秒内调用 startForeground()
        Notification notification = createNotification();
        startForeground(NOTIFICATION_ID, notification);
        // 执行你的后台任务
        performBackgroundWork();

        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        Log.d(TAG, "onBind: " + intent.getAction());
        return mICalculator;
    }

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                    CHANNEL_ID1,
                    "后台服务通道",
                    NotificationManager.IMPORTANCE_LOW
            );
            channel.setDescription("用于后台服务的通知通道");

            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(channel);
        }
    }

    private Notification createNotification() {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID1)
                .setContentTitle("后台服务运行中")
                .setContentText("服务正在后台执行任务")
                .setPriority(NotificationCompat.PRIORITY_LOW)
                .setOngoing(true);

        return builder.build();
    }

    private void performBackgroundWork() {
        // 在这里执行你的后台任务
        // 例如：网络请求、数据同步、位置跟踪等
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // 清理工作
    }
}